// pages/question/view.js
var app = getApp();
var WxParse = require('../../vendor/wxParse/wxParse.js');
Page({
  data:{
      inputOptions:{},
      question:[],
      answers:[],
      editAnswer:'',
      isEdit:false,
      answerInput:'',
      is_answer:false,
      userInfo: {}
      },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
    var that = this;
    that.setData({
        inputOptions: options
    });
    app.getUserInfo(function (userInfo) {
            //更新数据
            that.setData({
                userInfo: userInfo
            });
        });
    getDetail(that,options.id);
    getAnswers(that,options.id);
  },
  onPullDownRefresh: function () {
        var that = this;
        wx.showNavigationBarLoading();
        this.setData({
            question: [],
            answers:[]
        });
        getDetail(that,that.data.inputOptions.id);
        getAnswers(that,that.data.inputOptions.id);
        wx.stopPullDownRefresh;
        wx.hideNavigationBarLoading();
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  },
  answer:function(e){
      var that = this;
      var answer=e.detail.value.answer;
      var questionId=e.detail.target.dataset.questionId;
      if(!questionId){
          
      }
      wx.request({
            url: 'https://api.cnblogs.com/api/questions/'+questionId+'/answers', //仅为示例，并非真实的接口地址
            method: 'POST',
            data: {
                "Answer": answer
            },
            header: {
                'Authorization': 'Bearer '+ app.accessToken
            },
            success: function (res) {
                getAnswers(that,questionId);
            },
            fail: function () {
                console.log('获取用户信息失败!');
            }
        });
  },
  patch:function(e){
      var that = this;
      var answer=e.detail.value.answer;
      wx.request({
            url: 'https://api.cnblogs.com/api/questions/'+that.data.editAnswer.questionId+'/answers/'+that.data.editAnswer.answerId, //仅为示例，并非真实的接口地址
            method: 'PATCH',
            data: {
                "Answer": answer
            },
            header: {
                'Authorization': 'Bearer '+ app.accessToken
            },
            success: function (res) {
                getAnswers(that,that.data.editAnswer.questionId);
            },
            fail: function () {
                console.log('获取用户信息失败!');
            }
        });
  },
  delete:function(e){
      var that = this;
      var questionId=e.target.dataset.questionId;
      var answerId=e.target.dataset.answerId;
      if(!answerId){
          return;
      }
      wx.request({
            url: 'https://api.cnblogs.com/api/questions/'+questionId+'/answers/'+answerId, //仅为示例，并非真实的接口地址
            method: 'DELETE',
            data: {
                
            },
            header: {
                'Authorization': 'Bearer '+ app.accessToken
            },
            success: function (res) {
                getAnswers(that,questionId); 
            },
            fail: function () {
                console.log('获取用户信息失败!');
            }
        });
  },
  edit: function (e) {
        var that = this;
        var data={};
        var oldContent = e.target.dataset.editAnswer;
        var questionId = e.target.dataset.questionId;
        var answerId = e.target.dataset.answerId;
        data.oldContent=oldContent;
        data.answerId=answerId;
        data.questionId=questionId;
        that.setData({
            isEdit: true,
            editAnswer: data
        });
    },
  listenAnswerInput:function(event){
      this.data.answerInput = event.detail.value;
  }
});
 //获取问题详情
function getDetail(that,question_id) {
    wx.request({
        url: 'https://api.cnblogs.com/api/questions/' + question_id, 
        method: 'GET',
        data: {
        },
        header: {
            'Authorization': 'Bearer ' + app.accessToken
        },
        success: function (res) {
            that.setData({
                question: res.data
            });
            WxParse.wxParse('ques_content', 'html', that.data.question.Content, that, 5);
        },
        fail: function () {
            console.log('获取用户信息失败!');
        }
    });
}
//获取问题的回答列表
function getAnswers(that,question_id){
    wx.request({
        url: 'https://api.cnblogs.com/api/questions/' + question_id + '/answers', 
        method: 'GET',
        data: {
        },
        header: {
            'Authorization': 'Bearer ' + app.accessToken
        },
        success: function (res) {
            var data = {};
            data.status = true;
            data.data = res.data;
            if (res.data.length < 1) {
                data.status = false;
            }
            that.setData({
                answers: data
            });
        },
        fail: function () {
            console.log('获取用户信息失败!');
        }
    });
}